package net.lab1024.sa.base.module.support.traffic.service;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.module.support.traffic.domain.dto.MqttHeartbeatDTO;
import net.lab1024.sa.base.module.support.traffic.domain.dto.MqttResponseDTO;
import org.springframework.stereotype.Component;

/**
 * MQTT消息处理器
 */
@Slf4j
@Component
public class TrafficMqttMessageHandler {

    @Resource
    private TrafficHistoryService trafficHistoryService;

    /**
     * 处理心跳消息
     */
    public void handleHeartbeat(String topic, MqttHeartbeatDTO heartbeatDTO) {
        log.info("收到设备心跳 - Topic: {}, DeviceMode: {}, Interval: {}, Timestamp: {}",
                topic, heartbeatDTO.getCurrentMode(), heartbeatDTO.getInterval(), heartbeatDTO.getTimestamp());
        trafficHistoryService.saveHeartbeat(topic, heartbeatDTO);
    }

    /**
     * 处理响应消息
     */
    public void handleResponse(String topic, MqttResponseDTO responseDTO) {
        log.info("收到设备响应 - Topic: {}, RequestId: {}, Status: {}, Message: {}",
                topic, responseDTO.getRequestId(), responseDTO.getStatus(), responseDTO.getMessage());
        trafficHistoryService.saveResponse(topic, responseDTO);
    }
}

